package com.wei.ssm.dao;

import com.wei.ssm.domain.Member;
import com.wei.ssm.domain.Product;
import com.wei.ssm.domain.Role;
import com.wei.ssm.domain.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IUserDao {

    @Select("select * from users where username = #{username} ")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "password", column = "password"),
            @Result(property = "phoneNum", column = "phoneNum"),
            @Result(property = "peopleCount", column = "peopleCount"),
            @Result(property = "status", column = "status"),
            @Result(property = "roles",column = "id",javaType =java.util.List.class,many = @Many(select = "com.wei.ssm.dao.IRoleDao.findRolesById2"))
    })
    UserInfo findByUserName(String username);

    @Select("select * from users")
    List<UserInfo> findAll();

    @Insert("insert into users(username,password,email,phoneNum,status) values(#{username},#{password},#{email},#{phoneNum},#{status})")
    void save(UserInfo user);


    @Select("select * from users where id=#{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "password", column = "password"),
            @Result(property = "phoneNum", column = "phoneNum"),
            @Result(property = "status", column = "status"),
            @Result(property = "roles",column = "id",javaType = java.util.List.class,many = @Many(select = "com.wei.ssm.dao.IRoleDao.findRolesById"))
    })
    UserInfo findById(int id);

    @Delete("delete from users_role where userId = #{id}")
    void deleteById(int id);

    @Select("select * from role where id not in(select roleId from users_role where userId = #{id})")
    List<Role> findUserByIdAndAllRole(int id);

    @Insert("insert into users_role(userId,roleId) values(#{userId},#{id})")
    void addRoleToUser(@Param("userId") int userId, @Param("id") int id);
}
